import random
from django.http import JsonResponse
from rest_framework.views import  APIView
from lxml import etree
from dal import models




class RadarData(APIView):

    def get(self, request):

        data = {}
        value = []
        time = ""
        indicator = []

        try:

            Remen = models.Remen.objects.order_by("rate").first()
            New = models.New.objects.order_by("rate").first()
            Jingdian = models.Jingdian.objects.order_by("rate").first()
            DoubanTop = models.DoubanTop.objects.order_by("rate").first()

            dict_r = {}
            dict_r['text'] = Remen.title
            dict_r['max'] = 10
            indicator.append(dict_r)
            value.append(Remen.rate)

            dict_r = {}
            dict_r['text'] = New.title
            dict_r['max'] = 10
            indicator.append(dict_r)
            value.append(New.rate)

            dict_r = {}
            dict_r['text'] = Jingdian.title
            dict_r['max'] = 10
            indicator.append(dict_r)
            value.append(Jingdian.rate)

            dict_r = {}
            dict_r['text'] = DoubanTop.title
            dict_r['max'] = 10
            indicator.append(dict_r)
            value.append(DoubanTop.rate)



            data['data'] = value
            data['time'] = models.History.objects.order_by("id").last().time.strftime('%Y-%m-%d')
            data['indicator'] = indicator
            return JsonResponse(data)

        except Exception as e:
            print(e)
            data['code'] = 444
            return JsonResponse(data)



